package com.explment.lens.utils.uuid;

import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

public class UUIDUtils {
    // 生成标准UUIDv4（带连字符）
    public static String randomUUID() {
        return UUID.randomUUID().toString();
    }

    // 生成简化UUID（无连字符）
    public static String simpleUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    // 高性能版本（使用ThreadLocalRandom）
    public static String fastUUID() {
        byte[] randomBytes = new byte[16];
        ThreadLocalRandom.current().nextBytes(randomBytes);
        randomBytes[6] &= 0x0f;  // 清除版本位
        randomBytes[6] |= 0x40;  // 设置为版本4
        randomBytes[8] &= 0x3f;  // 清除变体位
        randomBytes[8] |= 0x80;  // 设置为RFC变体
        return new UUID(
                bytesToLong(randomBytes, 0),
                bytesToLong(randomBytes, 8)
        ).toString();
    }

    // 高性能简化版
    public static String fastSimpleUUID() {
        return fastUUID().replace("-", "");
    }

    private static long bytesToLong(byte[] bytes, int offset) {
        long result = 0;
        for (int i = 0; i < 8; i++) {
            result = (result << 8) | (bytes[offset + i] & 0xff);
        }
        return result;
    }

//    public static void main(String[] args) {
//        System.out.println("标准UUIDv4: " + randomUUID());
//        System.out.println("简化UUID: " + simpleUUID());
//        System.out.println("高性能UUID: " + fastUUID());
//        System.out.println("高性能简化版UUID: " + fastSimpleUUID());
//        System.out.println("高性能简化版UUID长度: " + fastSimpleUUID().length());
//        System.out.println("高性能简化版UUID是否为32位: " + (fastSimpleUUID().length() == 32));
//
//    }
}
